package com.hhit.bigscreenbackmanage.service.impl.tgygl;

import com.hhit.bigscreenbackmanage.dao.tgygl.TTgyglGcjzMapper;
import com.hhit.bigscreenbackmanage.entity.tgygl.TTgyglGcjz;
import com.hhit.bigscreenbackmanage.service.tgygl.TTgyglGcjzService;
import org.seimicrawler.xpath.JXNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 *  工程进展
 */
@Service
public class TTgyglGcjzServiceImpl implements TTgyglGcjzService {

    @Autowired
    private TTgyglGcjzMapper tTgyglGcjzMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insert(TTgyglGcjz record) {
        return 0;
    }

    @Override
    public int insertSelective(TTgyglGcjz record) {
        return 0;
    }

    @Override
    public TTgyglGcjz selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(TTgyglGcjz record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(TTgyglGcjz record) {
        return 0;
    }

//    @Override
//    public int insert(List<JXNode> jxNodeList, int id) throws Exception {
//        return 0;
//    }

    private void initList(List<JXNode> khJxnodeList, int id) {
        AtomicInteger oneKhOnePointIndex = new AtomicInteger();  //1.在文档中的位置
        AtomicInteger oneKhTwoPointIndex = new AtomicInteger();  //2.在文档中的位置
        AtomicInteger oneKhThreePointIndex = new AtomicInteger();  //3.在文档中的位置
        AtomicInteger oneKhFourPointIndex = new AtomicInteger();  //4.在文档中的位置
        List<JXNode> finalOneKhJxnodeList = khJxnodeList;
        finalOneKhJxnodeList.forEach(item-> {
            if (item.toString().indexOf("1. ") != -1) { //
                System.out.println(item.toString());
                oneKhOnePointIndex.set(finalOneKhJxnodeList.indexOf(item));
            }
            if (item.toString().indexOf("2. ") != -1) { //
                System.out.println(item.toString());
                oneKhTwoPointIndex.set(finalOneKhJxnodeList.indexOf(item));
            }
            if (item.toString().indexOf("3. ") != -1) { //
                System.out.println(item.toString());
                oneKhThreePointIndex.set(finalOneKhJxnodeList.indexOf(item));
            }
            if (item.toString().indexOf("4. ") != -1) { //
                System.out.println(item.toString());
                oneKhFourPointIndex.set(finalOneKhJxnodeList.indexOf(item));
            }
        });

        List<JXNode> oneKhOnePointJxnList = null;
        List<JXNode> oneKhTwoPointJxnList = null;
        List<JXNode> oneKhThreePointJxnList = null;
        List<JXNode> oneKhFourPointJxnList = null;


        if (oneKhOnePointIndex.get()!=0) { //有1.
            if (oneKhTwoPointIndex.get()!=0) { //有1.~2.
                oneKhOnePointJxnList = finalOneKhJxnodeList.subList(oneKhOnePointIndex.get(), oneKhTwoPointIndex.get());
            } else { //有1.无2. 无(二)
                oneKhOnePointJxnList = finalOneKhJxnodeList.subList(oneKhOnePointIndex.get(), finalOneKhJxnodeList.size());
            }
        }
        // System.out.println(oneKhOnePointJxnList);

        if (oneKhTwoPointIndex.get()!=0) {  //有2.
            if (oneKhThreePointIndex.get()!=0) { //有2.~3.
                oneKhTwoPointJxnList = finalOneKhJxnodeList.subList(oneKhTwoPointIndex.get(), oneKhThreePointIndex.get());
            } else { //有2.无3. 无(二)
                oneKhTwoPointJxnList = finalOneKhJxnodeList.subList(oneKhTwoPointIndex.get(), finalOneKhJxnodeList.size());
            }
        }
        // System.out.println(oneKhTwoPointJxnList);

        if (oneKhThreePointIndex.get()!=0) {  //有3.
            if (oneKhFourPointIndex.get()!=0) { //有3.~4.
                oneKhThreePointJxnList = finalOneKhJxnodeList.subList(oneKhThreePointIndex.get(), oneKhFourPointIndex.get());
            } else { //有3.无4. 无(二)
                oneKhThreePointJxnList = finalOneKhJxnodeList.subList(oneKhThreePointIndex.get(), finalOneKhJxnodeList.size());
            }
        }
        // System.out.println(oneKhThreePointJxnList);

        if (oneKhFourPointIndex.get()!=0) {  //有4.
            oneKhFourPointJxnList = finalOneKhJxnodeList.subList(oneKhFourPointIndex.get(), finalOneKhJxnodeList.size());
        }
        // System.out.println(oneKhFourPointJxnList);

        // }

        insertItem(oneKhOnePointJxnList, finalOneKhJxnodeList.get(0).toString(), id);
        insertItem(oneKhTwoPointJxnList, finalOneKhJxnodeList.get(0).toString(), id);
        insertItem(oneKhThreePointJxnList, finalOneKhJxnodeList.get(0).toString(), id);
        insertItem(oneKhFourPointJxnList, finalOneKhJxnodeList.get(0).toString(), id);

        //
        if (oneKhOnePointJxnList==null&&oneKhTwoPointJxnList==null
                &&oneKhThreePointJxnList==null
                &&oneKhFourPointJxnList==null) {
            insertItem(finalOneKhJxnodeList, finalOneKhJxnodeList.get(0).toString(), id);
        }
    }

    private void insertItem(List<JXNode> jxNodeList, String majorProjectName, int id) {
        AtomicInteger jinchangIndex = new AtomicInteger();  //进场在文档中的位置
        AtomicInteger jichuIndex = new AtomicInteger();  //基础在文档中的位置
        AtomicInteger litaIndex = new AtomicInteger();  //立塔在文档中的位置
        AtomicInteger jiaxianIndex = new AtomicInteger();  //架线在文档中的位置

        if (jxNodeList!=null&&jxNodeList.size()>0) {  //处理(一)、1.
            List<JXNode> finalOneKhOnePointJxnList = jxNodeList;
            TTgyglGcjz tTgyglGcjzOneKhOnePointBean = new TTgyglGcjz();

            finalOneKhOnePointJxnList.forEach(item-> {
                if (item.toString().indexOf("进场：") != -1) { //
                    jinchangIndex.set(finalOneKhOnePointJxnList.indexOf(item));
                }
                if (item.toString().indexOf("基础：") != -1) { //
                    jichuIndex.set(finalOneKhOnePointJxnList.indexOf(item));
                }
                if (item.toString().indexOf("立塔：") != -1) { //
                    litaIndex.set(finalOneKhOnePointJxnList.indexOf(item));
                }
                if (item.toString().indexOf("架线：") != -1) { //
                    jiaxianIndex.set(finalOneKhOnePointJxnList.indexOf(item));
                }
            });
            tTgyglGcjzOneKhOnePointBean.setMajorProjectName(majorProjectName);
            tTgyglGcjzOneKhOnePointBean.setProjectName(finalOneKhOnePointJxnList.get(0).toString());
            if (jinchangIndex.get()==0) { //进场==0
                String wcqkStr = finalOneKhOnePointJxnList.subList(1,finalOneKhOnePointJxnList.size()).toString();
                tTgyglGcjzOneKhOnePointBean.setWcqk(wcqkStr.substring(1,wcqkStr.length()-1));
            } else { //进场==1
                tTgyglGcjzOneKhOnePointBean.setJinchang(finalOneKhOnePointJxnList.get(jinchangIndex.get()).toString());
            }
            if (jichuIndex.get()!=0) { //基础!=0
                tTgyglGcjzOneKhOnePointBean.setJichu(finalOneKhOnePointJxnList.get(jichuIndex.get()).toString());
            }
            if (litaIndex.get()!=0) { //立塔!=0
                tTgyglGcjzOneKhOnePointBean.setLita(finalOneKhOnePointJxnList.get(litaIndex.get()).toString());
            }
            if (jiaxianIndex.get()!=0) { //架线!=0
                tTgyglGcjzOneKhOnePointBean.setJiaxian(finalOneKhOnePointJxnList.get(jiaxianIndex.get()).toString());
            }
            tTgyglGcjzOneKhOnePointBean.setFileId(id);
            tTgyglGcjzMapper.insert(tTgyglGcjzOneKhOnePointBean);
        }
    }

    @Override
    public int insert(List<JXNode> jxNode, int id) throws Exception {
        System.out.println(id);

        AtomicInteger yfgcjzIndex = new AtomicInteger();  //x月份工程进展位置
        String nextFileIndexStr = "二、";    // 下一项在文档中的位置标识
        AtomicInteger nextFileIndex = new AtomicInteger();  //下一项在文档中的位置

        jxNode.forEach(item->{
            if (item.toString().indexOf("月份工程进展")!=-1) { // nextFileIndex
                yfgcjzIndex.set(jxNode.indexOf(item));
            }
            if (item.toString().indexOf("二、")!=-1) { // nextFileIndex
                nextFileIndex.set(jxNode.indexOf(item));
            }
        });

        //截取出 x月份工程进展 JXNodeList
        List<JXNode> gcjzJxnList = jxNode.subList(yfgcjzIndex.get(), nextFileIndex.get());
        // System.out.println(gcjzJxnList);

        //根据(一)、(二)、(三)、(四)....进行拆分
        AtomicInteger oneKhIndex = new AtomicInteger();  //(一)在文档中的位置
        AtomicInteger twoKhIndex = new AtomicInteger();  //(二)在文档中的位置
        AtomicInteger threeKhIndex = new AtomicInteger();  //(三)在文档中的位置
        AtomicInteger fourKhIndex = new AtomicInteger();  //(四)在文档中的位置

        gcjzJxnList.forEach(item->{
            if (item.toString().indexOf("（一）")!=-1) { //
                oneKhIndex.set(gcjzJxnList.indexOf(item));
            }
            if (item.toString().indexOf("（二）")!=-1) { //
                twoKhIndex.set(gcjzJxnList.indexOf(item));
            }
            if (item.toString().indexOf("（三）")!=-1) { //
                threeKhIndex.set(gcjzJxnList.indexOf(item));
            }
            if (item.toString().indexOf("（四）")!=-1) { //
                fourKhIndex.set(gcjzJxnList.indexOf(item));
            }
        });

        List<JXNode> oneKhJxnodeList = null;
        if (oneKhIndex.get()!=0&&twoKhIndex.get()==0) { //只有(一)没有(二)
            oneKhJxnodeList = gcjzJxnList.subList(oneKhIndex.get(), gcjzJxnList.size());
        } else { //有(一)有(二)
            oneKhJxnodeList = gcjzJxnList.subList(oneKhIndex.get(), twoKhIndex.get());
        }
        if (oneKhJxnodeList!=null) {
            initList(oneKhJxnodeList, id);
        }

        List<JXNode> twoKhJxnodeList = null;
        if (twoKhIndex.get()!=0&&threeKhIndex.get()==0) { //只有(二)没有(三)
            twoKhJxnodeList = gcjzJxnList.subList(twoKhIndex.get(), gcjzJxnList.size());
        } else { //有(二)有(三)
            twoKhJxnodeList = gcjzJxnList.subList(twoKhIndex.get(), threeKhIndex.get());
        }
        if (twoKhJxnodeList!=null&&twoKhJxnodeList.size()!=0) {
            initList(twoKhJxnodeList, id);
        }

        //if (oneKhIndex.get()!=0&&twoKhIndex.get()==0) { //只有(一)没有(二)
//            List<JXNode> oneKhJxnodeList = gcjzJxnList.subList(oneKhIndex.get(), gcjzJxnList.size());
            // 根据1./2./3./4./....进行拆分



//        TTgyglGcjz bhtjstgyzlgcYchlzgc = new TTgyglGcjz();      //白鹤滩～江苏特高压直流工程-虞城换流站工程
//        bhtjstgyzlgcYchlzgc.setMajorProjectName(jxNode.get(2).toString()); //（一）白鹤滩～江苏特高压直流工程
//        bhtjstgyzlgcYchlzgc.setProjectName(jxNode.get(3).toString()); // 1. 虞城换流站工程（截至6月30日）
//        bhtjstgyzlgcYchlzgc.setWcqk(jxNode.get(4).toString()); // 场平工程累计完成99%（除1户未拆迁外全部完成）；桩基工程累计完成98.4%（除1户未拆迁和受文物发掘影响区域外全部完成）；土建工程累计完成28%，综合楼、2座辅控楼主体结构封顶，主控楼、2座辅控楼主体结构完成80%，高低端防火墙主体结构完成40%，低端阀厅开始钢结构吊装。
//        bhtjstgyzlgcYchlzgc.setFileId(id);
//        //insert
//        tTgyglGcjzMapper.insert(bhtjstgyzlgcYchlzgc);
//
//        TTgyglGcjz bhtjstgyzlgcZlxlgc = new TTgyglGcjz(); //白鹤滩～江苏特高压直流工程-直流线路工程
//        bhtjstgyzlgcZlxlgc.setMajorProjectName(jxNode.get(2).toString()); //（一）白鹤滩～江苏特高压直流工程
//        bhtjstgyzlgcZlxlgc.setProjectName(jxNode.get(5).toString()); // 2. 直流线路工程（截至6月30日）
//        bhtjstgyzlgcZlxlgc.setJinchang(jxNode.get(6).toString()); // 进场：省内全线487基，累计具备进场条件396基，占比81%，其中南京完成92%，常州完成86%，无锡完成73%，苏州完成100%。
//        bhtjstgyzlgcZlxlgc.setJichu(jxNode.get(7).toString()); // 基础：累计完成334基，占比69%，其中天送标段130基，占比69%，省送标段204基，占比68%。
//        bhtjstgyzlgcZlxlgc.setLita(jxNode.get(8).toString()); // 立塔：累计完成44基，占比9%，其中天送标段5基，占比3%，省送标段39基，占比13%。
//        bhtjstgyzlgcZlxlgc.setJiaxian(jxNode.get(9).toString()); // 架线：省送标段完成提前架线段架线7.728km，占比3.5%。
//        //insert
//        bhtjstgyzlgcZlxlgc.setFileId(id);
//        tTgyglGcjzMapper.insert(bhtjstgyzlgcZlxlgc);
//
//        TTgyglGcjz bhtjstgyzlgcJdjjqxlgc = new TTgyglGcjz();      //白鹤滩～江苏特高压直流工程-接地极及其线路工程
//        bhtjstgyzlgcJdjjqxlgc.setMajorProjectName(jxNode.get(2).toString()); //（一）白鹤滩～江苏特高压直流工程
//        bhtjstgyzlgcJdjjqxlgc.setProjectName(jxNode.get(10).toString()); // 3. 接地极及其线路工程（截至6月30日）
//        bhtjstgyzlgcJdjjqxlgc.setJinchang(jxNode.get(11).toString()); // 进场：全部77基，累计具备进场条件56基（含已完成），占比73%，其中常州完成88%、无锡完成66%。
//        bhtjstgyzlgcJdjjqxlgc.setJichu(jxNode.get(12).toString()); // 基础：累计完成35基，占比45%。
//        //insert
//        bhtjstgyzlgcJdjjqxlgc.setFileId(id);
//        tTgyglGcjzMapper.insert(bhtjstgyzlgcJdjjqxlgc);
//
//
//        TTgyglGcjz pt500qfscgc = new TTgyglGcjz();    //（二）配套500千伏送出工程（截至6月30日）
//        pt500qfscgc.setMajorProjectName(jxNode.get(13).toString()); //（二）配套500千伏送出工程（截至6月30日）
//        pt500qfscgc.setJinchang(jxNode.get(14).toString()); // 进场：全部374基，累计具备进场条件59基，占比15.8%，其中苏州完成15.4%，无锡完成19%。
//        pt500qfscgc.setJichu(jxNode.get(15).toString()); // 基础：累计完成19基（占比5.1%），其中1标段（安能二局）完成7基，占比6.9%，2标段（省送）完成2基，占比2.9%，3标段（电建一公司）完成6基，占比3.7%，4标段（南通送变电）完成4基，占比9.5%。
//        pt500qfscgc.setFileId(id);
//        tTgyglGcjzMapper.insert(pt500qfscgc);





        //List<JXNode> trNodeList = jxNode.sel("//tr");
//        trNodeList.forEach(item-> {
//            int index = trNodeList.indexOf(item);
//            if (index!=0) {
//                List<JXNode> tdNodeList = item.sel("//td");
//                TTgyglGcjz  tTgyglGcjz = new TTgyglGcjz();
//
//                tTgyglGcjz.setFileId(id);
//                tTgyglGcjzMapper.insert(tTgyglGcjz);
//            }
//            System.out.println(item);
//        });
        return 0;
    }

    @Override
    public List<TTgyglGcjz> selectListByFileId(Integer fileId) {
        List<TTgyglGcjz> resulList = null;
        TTgyglGcjz tTgyglGcjz = new TTgyglGcjz();
        tTgyglGcjz.setFileId(fileId);
        try {
            resulList = tTgyglGcjzMapper.selectByBean(tTgyglGcjz);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resulList;
    }
}
